/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.barrientosjunior.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 *
 * @author Junior
 */
public class Files {

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if(sourceFile.isDirectory()){
            if (!destFile.exists()){
                destFile.mkdir();
            }
            String files[] = sourceFile.list();

            for(int i = 0; i < files.length; i++){
                copyFile(new File(sourceFile, files[i]), new File(destFile, files[i]));
            }
        } else {
            if(!sourceFile.getName().endsWith(".db")){
                if (!destFile.exists()) {
                    destFile.createNewFile();
                }

                FileChannel source = null;
                FileChannel destination = null;
                try {
                    source = new FileInputStream(sourceFile).getChannel();
                    destination = new FileOutputStream(destFile).getChannel();
                    destination.transferFrom(source, 0, source.size());
                } finally {
                    if (source != null) {
                        source.close();
                    }
                    if (destination != null) {
                        destination.close();
                    }
                }
            }
        }
    }
}
